import Vue from 'vue'
import Router from 'vue-router'
import Index from '../pages/Index'
import Login from '../pages/Login'
import Register from '../pages/Register'
import ApplyMeeting from '../pages/ApplyMeeting'
import Workspace from '../pages/Workspace'
import InviteUsers from '../pages/InviteUsers'
import ADiscussion from '../pages/ADiscussion'
import News from '../pages/News'
import NewInvitation from '../pages/Components/NewInvitation'
import Welcome from '../pages/Welcome'
import RebuttalDashboard from '../pages/Components/RebuttalDashboard'
import Review from '../pages/Review'
import ReviewDashboard from '../pages/Components/ReviewDashboard'
import InvitationISent from '../pages/Components/InvitationISent'
import MyContributionDashboard from '../pages/Components/MyContributionDashboard'
import Rebuttal from '../pages/Rebuttal'
import AdminApprove from '../pages/AdminApprove'
import ReviewStatusInThisConference from '../pages/Components/ReviewStatusInThisConference'
import Contribute from '../pages/Contribute'
import MyContribution from '../pages/MyContribution'
import CreateDiscussion from '../pages/CreateDiscussion'
import DiscussionDashboard from '../pages/DiscussionDashboard'

// import ShowMap from '../pages/ShowMap'
// import LatLugBounds from '../../node_modules/_vue2-google-maps@0.10.7@vue2-google-maps/examples/components/02LatLngBounds'
import store from '../store'

Vue.use(Router)

export const router = new Router({
  linkExactActiveClass: 'active',
  routes: [
    {
      path: '/',
      name: 'index',
      components: { default: Index},
      meta: { title: 'Index' },
      children: [
        {
          path: '/',
          name: 'welcome',
          component: Welcome
        },
        {
          // 当 /foo/:id/profile 匹配成功，
          // FooProfile 会被渲染在 Foo的 <router-view> 中
          path: 'news',
          name: 'news',
          component: News,
          meta: {
            title: 'News'
          }
        },
        {
          path: 'login',
          name: 'login',
          component: Login
        },
        {
          path: 'register',
          name: 'register',
          component: Register
        },
        {
          // 当 /foo/:id/profile 匹配成功，
          // FooProfile 会被渲染在 Foo的 <router-view> 中
          path: 'applyMeeting',
          name: 'applyMeeting',
          component: ApplyMeeting,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'ApplyMeeting'
          }
        },
        {
          path: 'myContribution',
          name: 'myContribution',
          component: MyContributionDashboard,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'MyContribution'
          }
        },

        {
          // 当 /foo/:id/profile 匹配成功，
          // FooProfile 会被渲染在 Foo的 <router-view> 中
          path: 'workspace',
          name: 'workspace',
          component: Workspace,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'WorkSpace'
          },
          children: [
            // {
            //   path: '/myContribution/:id',
            //   name: 'myContribution',
            //   component: MyContribution
            // },

          ],
        },
        {
          // 当 /foo/:id/profile 匹配成功，
          // FooProfile 会被渲染在 Foo的 <router-view> 中
          path: 'invite/:id',
          name: 'inviteUsers',
          component: InviteUsers,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'Invite'
          }
        },
        {
          path: 'reviewStatus/:id',
          name: 'reviewStatus',
          component: ReviewStatusInThisConference,
          meta: {
            title: 'ReviewStatus',
            requireAuth: true, // 需要登录权限
          }
        },
        {
          path: 'adminApprove',
          name: 'adminApprove',
          component: AdminApprove,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'AdminApprove'
          }
        },
        {
          path: 'contribute/:id',
          name: 'contribute',
          component: Contribute,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'Contribute'
          }
        },
        // {
        //   path: 'google',
        //   name: 'google',
        //   component: ShowMap
        // },
        // {
        //   path: 'test',
        //   name: 'test',
        //   component: LatLugBounds
        // },
        {
          path: 'workspace/myContribution/:id',
          name: 'myContribution',
          component: MyContribution,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'MyContribution'
          }
        },
        {
          path: 'workspace/review/:id',
          name: 'review',
          component: Review,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'Review'
          }
        },
        {
          path: '/newInvitation',
          name: 'newInvitation',
          component: NewInvitation,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'NewInvitation'
          }
        },
        {
          path: '/reviewDashboard',
          name: 'reviewDashboard',
          component: ReviewDashboard,
          meta: {
            requireAuth: true, // 需要登录权限
            title: 'ReviewDashboard'
          }
        },
        {
          path: '/invitationISent',
          name: 'invitationISent',
          component: InvitationISent,
          meta: {
            requireAuth: true, // 需要登录权限
          }
        },
        {
          path: '/createDiscussion',
          name: 'createDiscussion',
          component: CreateDiscussion,
          // meta: {
            // requireAuth: true, // 需要登录权限
          // }
        },
        {
          path: '/discussion',
          name: 'discussion',
          component: DiscussionDashboard,
          // meta: {
          // requireAuth: true, // 需要登录权限
          // }
        },
        {
          path: '/rebuttalDashboard',
          name: 'rebuttalDashboard',
          component: RebuttalDashboard,
          // meta: {
          // requireAuth: true, // 需要登录权限
          // }
        },
        {
          path: '/rebuttal/:id',
          name: 'rebuttal',
          component: Rebuttal,
          // meta: {
          // requireAuth: true, // 需要登录权限
          // }
        },
        {
          path: '/adiscussion/:id',
          name: 'adiscussion',
          component: ADiscussion,
          // meta: {
          // requireAuth: true, // 需要登录权限
          // }
        }
      ]
    }
  ]
})

// 前端登录拦截
router.beforeEach(function (to, from, next) {
  if (to.matched.some(record => record.meta.requireAuth)) {
    if (store.state.token) {
      if(to.path === '/login') {
        // alert('无需重复登录')
        next({
          path: '/workspace'
        })
      }
      if(to.path === "/adminApprove"){
        if(store.state.userName === 'admin'){
          next()
        } else {
          // alert("没有管理员权限")
          next({
            path: '/workspace'
          })
        }
      } else if(store.state.userName === 'admin') {
        if(to.path !== "/adminApprove" || to.path !== "/"){
          // alert('Administer has not credential')
          next({
            path: '/adminApprove'
          })
        } else next()
      }
      else next()
    }
    else {
      // alert('You Need Login First')
      next({
        path: '/login',
        query: {redirect: to.fullPath} // 登录成功之后重新跳转到该路由
      })
    }
  } else {
    next()
  }
})
